package com.atguigu.mediator.smartHouse;

import java.util.HashMap;

/**
 * @program: design-patterns
 * @author: yangshen
 * @description:
 * @create: 2022-03-09 10:44
 **/
public class ConcreteMediator extends Mediator{

    /**
     *集合，放入所有的同事对象
     */
    private HashMap<String, Colleague> colleagueMap;
    private HashMap<String, String> interMap;

    public ConcreteMediator() {
        this.colleagueMap = new HashMap<>();
        this.interMap = new HashMap<>();
    }

    @Override
    public void register(String colleagueName, Colleague colleague) {
        colleagueMap.put(colleagueName, colleague);

        if (colleague instanceof Alarm){
            interMap.put("Alarm", colleagueName);
        }else if (colleague instanceof CoffeeMachine){
            interMap.put("CoffeeMachine", colleagueName);
        }else if (colleague instanceof Tv){
            interMap.put("Tv", colleagueName);
        }else if (colleague instanceof Curtains){
            interMap.put("Curtains", colleagueName);
        }
    }

    /**
     * 具体中介者的核心方法
     * 1.根据得到消息，完成对应任务
     * 2.中介者在这个方法，协调各个具体的同事对象，完成任务
     * @param stageChange 某一同事类的不同任务
     * @param colleagueName 同事类名称
     */
    @Override
    public void getMessage(int stageChange, String colleagueName) {
        //处理闹铃发出的消息
        if (colleagueMap.get(colleagueName) instanceof Alarm){
            if (stageChange == 0){
                ((CoffeeMachine) colleagueMap.get(interMap.get("CoffeeMachine"))).startCoffee();
                ((Tv)colleagueMap.get(interMap.get("Tv"))).StartTv();
            }else if (stageChange == 1){
                ((Tv)colleagueMap.get(interMap.get("Tv"))).StopTv();
            }
        //处理咖啡机的消息
        }else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine){
            ((Curtains)colleagueMap.get(interMap.get("Curtains"))).upCurtains();
        }else if (colleagueMap.get(colleagueName) instanceof Tv){
            //如果是以电视机发出的消息，这里处理...
        }else if (colleagueMap.get(colleagueName) instanceof Curtains){
            //如果是以窗帘发出的消息，这里处理...
        }
    }

    @Override
    public void sendMessage() {

    }
}
